<template>
    <a-modal :title="$t('加入AD域')" v-model='isShow' @cancel='handleClose' width='30%' @ok='handleCreate'
        :cancelText="$t('Cancel')" :okText="$t('Sure')" :maskClosable='false' v-dragModal>
        <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
            <a-spin :spinning="loading">
                <a-form-model-item :label="$t('接入名称')" prop="name">
                    <a-input v-model="form.name" :placeholder="$t('Enter') + $t('接入名称')" />
                </a-form-model-item>
                <a-form-model-item :label="$t('域管域名')" prop="domain_name">
                    <a-input v-model="form.domain_name" :placeholder="$t('Enter') + $t('域管域名')" />
                </a-form-model-item>
                <a-form-model-item label="NetBIOS" prop="net_bois">
                    <a-input v-model="form.net_bois" :placeholder="$t('Enter') + 'NetBIOS'" />
                </a-form-model-item>
                <a-form-model-item label="DNS IP" prop="dns_ip">
                    <a-input v-model="form.dns_ip" :placeholder="$t('Enter') + 'DNS IP'" />
                </a-form-model-item>
                <a-form-model-item :label="$t('Username')" prop="user">
                    <a-input v-model="form.user" :placeholder="$t('Enter') + $t('Username')" />
                </a-form-model-item>
                <a-form-model-item :label="$t('Password')" prop="passwd">
                    <a-input-password v-model="form.passwd" :placeholder="$t('Enter') + $t('Password')" />
                </a-form-model-item>
            </a-spin>
        </a-form-model>
    </a-modal>
</template>

<script>
export default {
    data() {
        return {
            isShow: false,
            loading: false,
            labelCol: { span: 4 },
            wrapperCol: { span: 20 },
            form: {
                name: '',
                domain_name: '',
                net_bois: '',
                dns_ip: '',
                user: '',
                passwd: '',
            },
            rules: {
                name: [
                    { required: true, message: this.$t('Enter') + this.$t('接入名称'), trigger: 'blur' },
                    { pattern: /^[A-Za-z0-9_]{3,32}$/, message: this.$t('支持大小写英文字母 + 数字 + 英文下划线，3-32 个字符'), trigger: 'blur' }
                ],
                domain_name: [
                    { required: true, message: this.$t('Enter') + this.$t('域管域名'), trigger: 'blur' },
                    { pattern:/^[A-Za-z](?![0-9]{1,15}$)[a-zA-Z0-9-_.]{1,15}$/, message: this.$t('请输入正确的域管域名'), trigger: 'blur' }
                ],
                net_bois: [
                    { required: true, message: this.$t('Enter') + 'NetBIOS', trigger: 'blur' },
                    { pattern: /^(?!\.)(?![0-9]{1,15}$)[a-zA-Z0-9-_.]{1,15}$/, message: this.$t('请输入正确的NetBIOS'), trigger: 'blur' }
                ],
                dns_ip: [
                    { pattern: /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/, message: this.$t('请输入正确的IP地址'), trigger: 'blur' }
                ],
                user: [
                    { required: true, message: this.$t('Enter') + this.$t('Username'), trigger: 'blur' },
                    { pattern: /^[A-Za-z0-9_]{3,32}$/, message: this.$t('支持大小写英文字母 + 数字 + 英文下划线，3-32 个字符'), trigger: 'blur' }
                ],
                passwd: [
                    { required: true, message: this.$t('Enter') + this.$t('Password'), trigger: 'blur' },
                    { min: 1, max: 32, message: this.$t('长度限制为1-32'), trigger: 'blur' },
                ],
            },
        };
    },
    methods: {
        handleCreate() {
            this.$refs.ruleForm.validate(async valid => {
                if (valid) {
                    this.loading = true
                    const { data } = await this.$api.authentication.AppendAuthentication(this.form)
                    this.model(data.id) //返回模板
                    this.notice(data.id, data.params, data.flag) //通知列表显示
                    if (data.flag == '0') {
                        this.$emit('get-list')
                        this.handleClose()
                    }
                    this.loading = false
                } else {
                    return false;
                }
            });
        },
        handleClose() {
            this.$refs['ruleForm'].resetFields();
            this.isShow = false
        },
    }
}
</script>
